package com.sctu.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.EMSApp.FamousOneInfoActivity;
import com.sctu.EMSApp.R;
import com.sctu.http.UrlConfig;
import com.sctu.adapter.FamousAdapter;
import com.sctu.bean.Famous;
import com.sctu.bean.FamousItem;
import com.sctu.httpvollry.GetVolleyHttp;
import com.sctu.systemconfiguration.GetWindowsize;


import java.util.List;


/**
 * Created by zhu on 2015/11/3.
 */
public class ScenictourSightFragment extends Fragment {

    private ListView listView;
    private ImageView headimageView;
    private View view;
    private List<FamousItem.Results> resultss;
    private LinearLayout linearLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_scenictour_sight, container, false);
        listView = (ListView) this.view.findViewById(R.id.famous_list);
        headimageView = (ImageView) this.view.findViewById(R.id.famous_head_img);
        linearLayout = (LinearLayout) this.view.findViewById(R.id.famous_linear);

        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) linearLayout.getLayoutParams();
        linearParams.height = (int) (height*0.33);


        initheadimg();
        initlistview();

        return view;

    }


    private void initlistview() {

        // 发起网络请求

        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_FAMOUS_LIST,
                listener, errorlistener);


    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String jsonstring) {
            Log.e("Json", jsonstring);

            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            final FamousItem results = JSON.parseObject(jsonstring, FamousItem.class);
            resultss = results.getResults();

            //建立适配器，并绑定数据源绑定适配器到listview
            listView.setAdapter(new FamousAdapter(getActivity(), resultss));
            //监听每一个listitem并控制相应的跳转
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.i("mm", " onItemClick ");
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), FamousOneInfoActivity.class);
                    intent.putExtra("id", resultss.get(position).getId());
                    getActivity().startActivity(intent);


                }
            });

        }

    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(getActivity(), "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();

        }
    };


    private void initheadimg() {

        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_FAMOUS_HEAD,
                listenerhead, errorlistenerhead);
    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listenerhead = new Response.Listener<String>() {

        @Override
        public void onResponse(String jsonstring) {
            Log.e("Json", jsonstring);

            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            Famous results = JSON.parseObject(jsonstring, Famous.class);
            List<Famous.Results> rr = results.getResults();
            //设置图片
            GetVolleyHttp.getInstance(getActivity()).setImageLoader(headimageView, R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + rr.get(0).getPath());
        }
    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistenerhead = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(getActivity(), "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();

        }
    };

}





